<template>
  <div class="container">
    <div class="login forget">
      <van-form
        @submit="onSubmit"
        ref="form"
        :validate-first="true"
        @failed="onFailed"
        :show-error-message="false"
      >
        <van-field
          v-model="mobile"
          name="mobile"
          label="手机号码"
          placeholder="请输入手机号码"
          :rules="[
            { required: true, message: '请填写手机号码' },
            {
              pattern: /^1[3456789]\d{9}$/,
              message: '请填写正确手机号码',
            },
          ]"
        />
        <van-field
          v-model="code"
          name="code"
          maxlength="6"
          label="短信验证码"
          placeholder="请输入验证码"
          :rules="[{ required: true, message: '请填写验证码' }]"
        >
          <template slot="right-icon">
            <div class="getCode" @click="getCode" v-if="!hasSendCode">
              获取验证码
            </div>
            <div class="sendCode" v-else>{{ second }}s</div>
          </template>
        </van-field>
        <van-field
          v-model="newPassword"
          name="newPassword"
          :type="passwordType"
          label="新密码"
          placeholder="请输入新密码"
          :rules="[{ required: true, message: '请填写新密码' }]"
          bind:click-icon="switchPasswordType"
        >
          <template slot="right-icon">
            <van-icon
              class="showIcon"
              :name="passwordType == 'text' ? 'closed-eye' : 'eye-o'"
              @click="switchPasswordType"
            />
          </template>
        </van-field>
        <div class="pwtips">
          密码长度在8位及以上，密码应包含数字，大小写字母，特殊字符的两种或两种以上
        </div>
        <div style="margin: 16px">
          <van-button
            color="#1E1F26"
            round
            block
            type="info"
            native-type="submit"
          >
            确定
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  forgetSendMobileCode_post,
  forgetCheckMobileCode_post,
} from '@/api/app/seller/login'
import { Toast } from 'vant'
export default {
  data() {
    return {
      mobile: '',
      code: '',
      hasSendCode: false,
      newPassword: '',
      second: 60,
      timer: null,
      passwordType: 'password',
    }
  },
  methods: {
    onFailed(values) {
      if (Array.isArray(values.errors)) {
        Toast.fail(values.errors[0].message)
      }
    },
    switchPasswordType() {
      this.passwordType =
        this.passwordType === 'password' ? 'text' : 'password'
    },
    onSubmit(values) {
      forgetCheckMobileCode_post({
        data: { ...values },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      }).then(() => {
        this.$notify({
          type: 'success',
          message: '修改密码成功',
          onClose: () => {
            this.$router.push('/login')
          },
        })
      })
    },
    getCode(e) {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          forgetSendMobileCode_post({
            data: {
              mobile: this.mobile,
              challenge: '111111',
            },
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          }).then((data) => {
            this.$toast('短信发送成功')
            this.hasSendCode = true
            this.timer = setInterval(() => {
              this.second--
              if (this.second === 0) {
                this.hasSendCode = false
                clearInterval(this.timer)
                this.second = 60
                this.timer = null
              }
            }, 1000)
          })
        })
        .catch((err) => {
          Toast.fail(err.message)
        })
    },
  },
}
</script>

<style lang="scss" src="./styles/index.scss" scoped></style>
